/***********************************************************************
 * Copyright (c)  2020,Shanghai AisinoChip Co.,Ltd .
 * All rights reserved.

 * Filename    : main.c
 * Description : Modules studying
 * Author(s)   : xwl     
 * version     : 1.0
 * Modify date : 2020-06-15  
 ***********************************************************************/

#include "app.h"  
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_bulk_transfer.h"  

#define UART_BAUD_RATE  115200

extern HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);  


UART_HandleTypeDef UART1_Handle; 

USBD_HandleTypeDef USBD_Device; 


void USBD_Setup_Process(PCD_HandleTypeDef *p_pcd )   
{  
    USB_OTG_GlobalTypeDef *USBx = p_pcd->Instance;
    uint32_t USBx_BASE = (uint32_t)USBx;
    USBD_SetupReqTypedef usb_request; 
    uint16_t length;  
    uint8_t  dev_addr, ret;   
    
    USBD_ParseSetupRequest(&usb_request, (uint8_t *)p_pcd->Setup);      
    
    switch (usb_request.bmRequest & 0x1FU)
    {   
        case USB_REQ_RECIPIENT_DEVICE:                        
        switch (usb_request.bmRequest & USB_REQ_TYPE_MASK) 
        {                
             case USB_REQ_TYPE_STANDARD:
             switch (usb_request.bRequest)
             {                                              
                case USB_REQ_GET_DESCRIPTOR:  
                            
                switch (usb_request.wValue >> 8)
                {                                                                                              
                    case USB_DESC_TYPE_DEVICE:  
                                                          
                    p_pcd->IN_ep[0].xfer_buff  = (uint8_t *)USBD_UMS_DeviceDescriptor_ex(&length);                      
                                             
                    USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);  
                        
                    break;  
                            
                    case USB_DESC_TYPE_CONFIGURATION:                                                    
                            
//                    if (p_pcd->Init.speed == USBD_HS_SPEED)
                   {
                       p_pcd->IN_ep[0].xfer_buff  = (uint8_t *)USBD_UMS_GetCfgDesc_ex(&length);                              
                   }
//                   else
//                   {
//                       p_pcd->IN_ep[0].xfer_buff  = (uint8_t *)USBD_UMS_GetFSCfgDesc_ex(&length);  
//                    }
                                                      
                    if (usb_request.wLength <= length)
                    {
                        length = usb_request.wLength;   
                    }
                            
                    USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);    
                            
                    break;  
                            
                    case USB_DESC_TYPE_STRING:                                        
                    ret = 0;                                      
                    switch ((uint8_t)(usb_request.wValue))
                    {                                                                                                                          
                        case USBD_IDX_LANGID_STR:
                        if (USBD_Device.pDesc->GetLangIDStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetLangIDStrDescriptor(USBD_Device.dev_speed, &length);
                        }
                        break;

                        case USBD_IDX_MFC_STR:
                        if (USBD_Device.pDesc->GetManufacturerStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetManufacturerStrDescriptor(USBD_Device.dev_speed, &length);
                        } 
                        break;

                        case USBD_IDX_PRODUCT_STR:
                        if (USBD_Device.pDesc->GetProductStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetProductStrDescriptor(USBD_Device.dev_speed, &length);
                        }
                        break;

                        case USBD_IDX_SERIAL_STR:
                        if (USBD_Device.pDesc->GetSerialStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetSerialStrDescriptor(USBD_Device.dev_speed, &length);
                        }
                        break;

                        case USBD_IDX_CONFIG_STR:
                        if (USBD_Device.pDesc->GetConfigurationStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetConfigurationStrDescriptor(USBD_Device.dev_speed, &length);
                        }
                        break;

                        case USBD_IDX_INTERFACE_STR:
                        if (USBD_Device.pDesc->GetInterfaceStrDescriptor != NULL)
                        {
                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetInterfaceStrDescriptor(USBD_Device.dev_speed, &length);
                        }
                        break; 
                                
                        default: 
                        ret = 1; 
                        USBD_CtlError(&USBD_Device, &usb_request);    
                        break;                                 
                    }  
                                        
                    if (0 == ret) 
                    {
                        USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);    
                    }                                            
                            
                    break;   
                            
                   case USB_DESC_TYPE_DEVICE_QUALIFIER:
                   if (USBD_Device.dev_speed == USBD_SPEED_HIGH)
                   {
                       p_pcd->IN_ep[0].xfer_buff = USBD_Device.pClass->GetDeviceQualifierDescriptor(&length);                                
                                
                       USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);  
                   }
                   else
                   {
                       USBD_CtlError(&USBD_Device, &usb_request);     
                   }
                   break;  
                            
                   case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION: 
//                   if (USBD_Device.dev_speed == USBD_SPEED_HIGH)
//                   {
//                        p_pcd->IN_ep[0].xfer_buff = USBD_Device.pClass->GetOtherSpeedConfigDescriptor(&length);
//                        p_pcd->IN_ep[0].xfer_buff[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION;
//                                
//                        USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);    
//                   }
//                   else
                   {
                       USBD_CtlError(&USBD_Device, &usb_request);    
                   }
                   break; 
                                        
                   default:
                   USBD_CtlError(&USBD_Device, &usb_request);    
                   break; 
               }
                        
               break;  // USB_REQ_GET_DESCRIPTOR  

               case USB_REQ_SET_ADDRESS: 
               dev_addr = (uint8_t)(usb_request.wValue) & 0x7FU;  
               USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
               USBx_DEVICE->DCFG |= ((uint32_t)dev_addr << 4) & USB_OTG_DCFG_DAD;  
                        
               USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);     
               USBD_Device.dev_state = USBD_STATE_ADDRESSED;   
               break;  
                        
               case USB_REQ_SET_CONFIGURATION:
                                               
               USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);  
               USBD_Device.pClass->Init(&USBD_Device, 0);  // init interrupt EP for HID 
               USBD_Device.dev_state = USBD_STATE_CONFIGURED; 
               break; 
                                    
               case USB_REQ_GET_STATUS:
               break;  
                                    
//            case USB_REQ_CLEAR_FEATURE:
//            break;  
//                                    
//            case USB_REQ_SET_FEATURE: 
//            break;  
                        
              default:
              USBD_CtlError(&USBD_Device, &usb_request);    
              break; 
          }
                                
          case USB_REQ_TYPE_CLASS:
          break;
                                
          default: 
          USBD_CtlError(&USBD_Device, &usb_request);    
          break; 
                                
       } // device request 
       break; 
                    
       case USB_REQ_RECIPIENT_INTERFACE: 
       switch (usb_request.bmRequest & USB_REQ_TYPE_MASK)
       {
           case USB_REQ_TYPE_CLASS :
            switch (usb_request.bRequest)
            {
                case GET_MAX_LUN: 
                {
                    uint8_t lun;
                                        
                    lun = 0;  
                    p_pcd->IN_ep[0].xfer_buff = &lun;                              
                                
                    USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, 1);      
                }
                break; 
                                    
                case Bulk_Only_Mass_Storage_Reset: 
                {
                    USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);    
                }
                break;   
                                    
                default:
                USBD_CtlError(&USBD_Device, &usb_request);    
                break; 
             }
             break;  
                         
             case USB_REQ_TYPE_STANDARD:  
             switch (usb_request.bRequest)
             {
                                    
                default:
                USBD_CtlError(&USBD_Device, &usb_request);    
                break; 
             }
             break; 
                                
             default: 
             USBD_CtlError(&USBD_Device, &usb_request);   
             break;                                       
          }
                    
             break;  // USB_REQ_RECIPIENT_INTERFACE  
                    
             case USB_REQ_RECIPIENT_ENDPOINT: 
             break;                   

       default:
       break;    
                    
    }
} 



/*********************************************************************************
* Function    : main
* Description : 
* Input       : 
* Outpu       : 
* Author      : xwl                        Date : 2022  
**********************************************************************************/
int main(void)
{
    uint32_t temp, USBx_BASE, USB_;   
    USB_OTG_EPTypeDef *ep; 
    USB_OTG_GlobalTypeDef *USB_Global_Regs;   
    PCD_HandleTypeDef * p_pcd;  
       
    HAL_Init();  
    SystemClock_Config(SYSCLK_220M_SRC_XTH_12M, PCLK1_DIV_SELECT, PCLK2_DIV_SELECT, PCLK3_DIV_SELECT, PCLK4_DIV_SELECT);      
    
    BSP_UART_Init(USART1, 115200);  
      
    __HAL_RCC_USB2_CLK_ENABLE();       
    __HAL_RCC_SYSCFG_CLK_ENABLE();   
   
    USBOTG2_PHYC->CR = (BIT0 | BIT5 | BIT4);   // USB2 Use default configure, 12M XTH, PLL, OTG  
                
    HAL_Delay(2);   // Wait PHY PLL to be stable    
    /* Init Device Library */
    USBD_Init(&USBD_Device, &UMS_Desc, USB_HS_OTG_ID2); 
    USB_Global_Regs = ((PCD_HandleTypeDef *)(USBD_Device.pData))->Instance;   
  
    /* Add Supported Class */
    USBD_RegisterClass(&USBD_Device, USBD_UMS_CLASS); 
    
    USBx_BASE = (uint32_t)USB_Global_Regs;   
    p_pcd = (PCD_HandleTypeDef *)USBD_Device.pData;    
    
    USBD_Device.dev_state = USBD_STATE_DEFAULT;     
  
    #ifdef USE_USB_INTERRUPT
    printfS("USB Interrupt Demo, Bulk Transfer, Max Packet is 512\n");  
    NVIC_EnableIRQ(USBOTG2_IRQn); 
    #else
    printfS("USB Poll Demo, Bulk Transfer, Max Packet is 512\n");    
    #endif 
    /* Start Device Process */   
    USBD_Start(&USBD_Device);   
   
    while(1) 
    {
      if (0 != USBD_EPx_Get_Out_EP_Error(p_pcd, UMS_EPOUT_ADDR) )
      {
          printfS("Out EP Error\n");      
          USBD_EPx_Clear_Out_EP_Error(p_pcd, UMS_EPOUT_ADDR);   
      }
      
      #ifndef USE_USB_INTERRUPT 
      USBD_LL_Bus_Activity(&USBD_Device);      
      
      if(USB_Global_Regs->GINTSTS & USB_OTG_GINTSTS_OEPINT)
      {
            uint32_t epint, ep_intr, epnum; 
                      
            epnum = 0U;  
            ep_intr = USB_ReadDevAllOutEpInterrupt(p_pcd->Instance);   
            
            while (ep_intr != 0U) 
            {
                if ((ep_intr & 0x1U) != 0U) 
                {   
                    epint = USB_ReadDevOutEPInterrupt(p_pcd->Instance, (uint8_t)epnum); 
            
                    if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
                    {
                        CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);  
                        
                        (void)PCD_EP_OutXfrComplete_int(p_pcd, epnum);  // for EP0, it is normally received zero length byte  
                    }

                    if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) // setup completely, ep0  
                    {
                        USBD_SetupReqTypedef usb_request; 
                        uint16_t length;  
                        uint8_t  dev_addr, ret;    
                        
                        CLEAR_OUT_EP_INTR(0, USB_OTG_DOEPINT_STUP);
                        /*setup phase done for previous decoded setup */                 
                        CLEAR_OUT_EP_INTR(0, USB_OTG_DOEPINT_STPKTRX);
                        USBD_ParseSetupRequest(&usb_request, (uint8_t *)p_pcd->Setup);    
                
                        
                        
                        switch (usb_request.bmRequest & 0x1FU)
                        {
                            case USB_REQ_RECIPIENT_DEVICE:                        
                            switch (usb_request.bmRequest & USB_REQ_TYPE_MASK) 
                            {                
                                case USB_REQ_TYPE_STANDARD:
                                switch (usb_request.bRequest)
                                {                                              
                                    case USB_REQ_GET_DESCRIPTOR:  
                                    printfS("GetDescriptor:\n");  
                                    switch (usb_request.wValue >> 8)
                                    {                                                                                              
                                        case USB_DESC_TYPE_DEVICE:  
                                                          
                                        p_pcd->IN_ep[0].xfer_buff  = (uint8_t *)USBD_UMS_DeviceDescriptor_ex(&length);                      
                                             
                                        USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);  
                        
                                        break;  
                            
                                        case USB_DESC_TYPE_CONFIGURATION:                                                    
                            
//                                        if (p_pcd->Init.speed == USBD_HS_SPEED)
                                        {
                                            p_pcd->IN_ep[0].xfer_buff  = (uint8_t *)USBD_UMS_GetCfgDesc_ex(&length);                              
                                        }
         
                                                      
                                        if (usb_request.wLength <= length)
                                        {
                                            length = usb_request.wLength;   
                                        }
                            
                                        USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);    
                            
                                        break;  
                            
                                        case USB_DESC_TYPE_STRING:                                        
                                        ret = 0;                                      
                                        switch ((uint8_t)(usb_request.wValue))
                                        {                                                                                                                          
                                            case USBD_IDX_LANGID_STR:
                                            if (USBD_Device.pDesc->GetLangIDStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetLangIDStrDescriptor(USBD_Device.dev_speed, &length);
                                            }
                                            break;

                                            case USBD_IDX_MFC_STR:
                                            if (USBD_Device.pDesc->GetManufacturerStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetManufacturerStrDescriptor(USBD_Device.dev_speed, &length);
                                            } 
                                            break;

                                            case USBD_IDX_PRODUCT_STR:
                                            if (USBD_Device.pDesc->GetProductStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetProductStrDescriptor(USBD_Device.dev_speed, &length);
                                            }
                                            break;

                                            case USBD_IDX_SERIAL_STR:
                                            if (USBD_Device.pDesc->GetSerialStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetSerialStrDescriptor(USBD_Device.dev_speed, &length);
                                            }
                                            break;

                                            case USBD_IDX_CONFIG_STR:
                                            if (USBD_Device.pDesc->GetConfigurationStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetConfigurationStrDescriptor(USBD_Device.dev_speed, &length);
                                            }
                                            break;

                                            case USBD_IDX_INTERFACE_STR:
                                            if (USBD_Device.pDesc->GetInterfaceStrDescriptor != NULL)
                                            {
                                                p_pcd->IN_ep[0].xfer_buff = USBD_Device.pDesc->GetInterfaceStrDescriptor(USBD_Device.dev_speed, &length);
                                            }
                                            break; 
                                
                                            default: 
                                            ret = 1; 
                                            USBD_CtlError(&USBD_Device, &usb_request);    
                                            break;                                 
                                        }  
                                        
                                        if (length > usb_request.wLength)
                                        {
                                            length = usb_request.wLength;   
                                        }
                                        if (0 == ret) 
                                        {
                                            USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);    
                                        }                                            
                            
                                        break;   
                            
                                        case USB_DESC_TYPE_DEVICE_QUALIFIER:
                                        if (USBD_Device.dev_speed == USBD_SPEED_HIGH)
                                        {
                                            p_pcd->IN_ep[0].xfer_buff = USBD_Device.pClass->GetDeviceQualifierDescriptor(&length);                                
                                
                                            USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, length);  
                                        }
                                        else
                                        {
                                           USBD_CtlError(&USBD_Device, &usb_request);     
                                        }
                                        break;  
                            
                                        case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION: 
   
                                        
                                        {
                                            USBD_CtlError(&USBD_Device, &usb_request);    
                                        }
                                        break; 
                                        
                                        default:
                                        USBD_CtlError(&USBD_Device, &usb_request);    
                                        break; 
                                    }
                        
                                    break;  // USB_REQ_GET_DESCRIPTOR  

                                    case USB_REQ_SET_ADDRESS: 
                                    printfS("SetAddress:\n"); 
                                    dev_addr = (uint8_t)(usb_request.wValue) & 0x7FU;  
                                    USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
                                    USBx_DEVICE->DCFG |= ((uint32_t)dev_addr << 4) & USB_OTG_DCFG_DAD;  
                        
                                    USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);     
                                    USBD_Device.dev_state = USBD_STATE_ADDRESSED;   
                                    break;  
                        
                                    case USB_REQ_SET_CONFIGURATION:
                                    printfS("SetConfig:\n");            
                                    USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);  
                                    USBD_Device.pClass->Init(&USBD_Device, 0);  // init interrupt EP for HID 
                                    USBD_Device.dev_state = USBD_STATE_CONFIGURED; 
                                    break; 
                                    
                                    case USB_REQ_GET_STATUS:
                                    break;  
                                    
                                    case USB_REQ_CLEAR_FEATURE:
                                    break;  
                                    
                                    case USB_REQ_SET_FEATURE: 
                                    printfS("SetFeature:0x%x, 0x%x\n", usb_request.wValue, usb_request.wIndex);  
                                           
                                    USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);  
                                    
                                    if (0x02 == usb_request.wValue) // Feature select = test mode  
                                    {
                                        *(uint32_t*)0x40080804 = (usb_request.wIndex >> 8) << 4;                                       
                                    }
                                    break;  
                        
                                    default:
                                    USBD_CtlError(&USBD_Device, &usb_request);    
                                    break; 
                                }
                                
                                case USB_REQ_TYPE_CLASS:
                                break;
                                
                                default: 
                                USBD_CtlError(&USBD_Device, &usb_request);    
                                break; 
                                
                            } // device request 
                            break; 
                    
                            case USB_REQ_RECIPIENT_INTERFACE: 
                            switch (usb_request.bmRequest & USB_REQ_TYPE_MASK)
                            {
                                case USB_REQ_TYPE_CLASS :
                                switch (usb_request.bRequest)
                                {
                                    case GET_MAX_LUN: 
                                    {
                                        uint8_t lun;
                                        
                                        lun = 0;  
                                        p_pcd->IN_ep[0].xfer_buff = &lun;                              
                                
                                        USBD_EP0_Send_Data(USBD_Device.pData, p_pcd->IN_ep[0].xfer_buff, 1);      
                                    }
                                    break; 
                                    
                                    case Bulk_Only_Mass_Storage_Reset: 
                                    {
                                        USBD_EP0_Send_Data(USBD_Device.pData, NULL, 0);    
                                    }
                                    break;   
                                    
                                    default:
                                    USBD_CtlError(&USBD_Device, &usb_request);    
                                    break; 
                                }
                                break;  
                         
                                case USB_REQ_TYPE_STANDARD:  
                                switch (usb_request.bRequest)
                                {
                                    
                                    default:
                                    USBD_CtlError(&USBD_Device, &usb_request);    
                                    break; 
                                }
                                break; 
                                
                                default: 
                                USBD_CtlError(&USBD_Device, &usb_request);   
                                break;                                       
                            }
                    
                            break;  // USB_REQ_RECIPIENT_INTERFACE  
                    
                            case USB_REQ_RECIPIENT_ENDPOINT: 
                            break;                   
                    
                        }
                              
                    }

                    if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
                    {
                        CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
                    }

                    /* Clear Status Phase Received interrupt */
                    if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
                    {
                        CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
                    }

                    /* Clear OUT NAK interrupt */
                    if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
                    {
                        CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);  
                    }
                } // if 
                
                epnum++;
                ep_intr >>= 1U;
                
            } // while 
        }  // process all OUT EP events      
        
        if(USB_Global_Regs->GINTSTS & USB_OTG_GINTSTS_IEPINT)  
        {
            uint32_t epint, ep_intr, epnum;       
            
            epnum = 0U;
            
            ep_intr = USB_ReadDevAllInEpInterrupt(p_pcd->Instance);    
            
            while (ep_intr != 0U)
            {
                if ((ep_intr & 0x1U) != 0U) /* In ITR */
                {
                    epint = USB_ReadDevInEPInterrupt(p_pcd->Instance, (uint8_t)epnum); 
            
                    if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC) 
                    {
                        if (0 != epnum)
                        {
                            USBD_Device.pClass->DataIn(&USBD_Device, epnum);   
                        }                      
                        CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC); 
                    }
            
                    if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
                    {
                        CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
                    }
                    if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
                    {
                        CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
                    }
                    if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
                    {
                        CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
                    }
                    if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
                    {
                        CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
                    }  
                    
                }
                epnum++;
                ep_intr >>= 1U;
            }
            
        }    // process all IN EP events    
    #endif  // USE_USB_INTERRUPT      
    } // check GINTSTS 
}



